﻿namespace PayPal.AdaptivePayments
{
    using System.Web.WebPages.Scope;
    using PayPal.Platform.SDK;

    public static class ImplicitSimplePay
    {
        public static PayResponse Execute(
                                    string receiverEmail,
                                    string senderEmail,
                                    decimal amount,
                                    string memo,
                                    string userIp,
                                    string deviceId,
                                    string ipnUrl = "",
                                    string currencyCode = "",
                                    string language = "")
        {
            if (string.IsNullOrEmpty(ipnUrl))
            {
                ipnUrl = Profile.IpnUrl;
            }

            if (string.IsNullOrEmpty(currencyCode))
            {
                currencyCode = Profile.CurrencyCode;
            }

            if (string.IsNullOrEmpty(language))
            {
                language = Profile.Language;
            }

            Validate(receiverEmail, senderEmail, amount, memo, userIp, deviceId, language, currencyCode);

            PayRequest payRequest = new PayRequest();
            
            // Implicit payments do not require cancel and returnUrl as there is 
            // no authorization required - this is a bug in Pay API
            payRequest.cancelUrl = "http://www.paypal.com";
            payRequest.returnUrl = "http://www.paypal.com";

            if (!string.IsNullOrEmpty(ipnUrl))
            {
                payRequest.ipnNotificationUrl = ipnUrl;
            }

            // Developer is the sender of the money
            payRequest.senderEmail = senderEmail;

            payRequest.clientDetails = new ClientDetailsType();
            payRequest.clientDetails.applicationId = Profile.FNProfile.ApplicationID;
            payRequest.clientDetails.deviceId = deviceId;
            payRequest.clientDetails.ipAddress = userIp;

            payRequest.actionType = "PAY";

            payRequest.currencyCode = currencyCode;
            payRequest.requestEnvelope = new RequestEnvelope();
            payRequest.requestEnvelope.errorLanguage = language;

            payRequest.receiverList = new Receiver[1];
            payRequest.receiverList[0] = new Receiver();
            payRequest.receiverList[0].amount = amount;
            payRequest.receiverList[0].email = receiverEmail;

            AdapativePayments ap = new AdapativePayments();
            ap.APIProfile = Profile.FNProfile;
            PayResponse response = ap.Pay(payRequest);

            if (ap.LastError != null)
            {
                throw ap.LastError;
            }

            return response;
        }

        private static void Validate(string receiverEmail, string senderEmail, decimal amount, string memo, string userIp, string deviceId, string language, string currencyCode)
        {
            ValidationHelper.ValidateProfile(Profile.FNProfile);
            ValidationHelper.ValidateLanguage(language);
            ValidationHelper.ValidateBaseInput(memo, userIp, deviceId);
            ValidationHelper.ValidateCurrencyCode(currencyCode);
            ValidationHelper.ValidateAccountEmail(senderEmail);
        }
    }
}